package com.cs307.redcross.client;

import java.io.Serializable;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Server implements EntryPoint, Serializable {

	private static final long serialVersionUID = -1265279476835788064L;
	public static Person currentUser;
	private final DatabaseHandlerServiceAsync databaseHandlerService = GWT.create(DatabaseHandlerService.class); 
	private final String relogString = "If this problem persists try relogging into the system.";
	private String sessionID = Cookies.getCookie("sid");
	
	RootPanel header;
	
	public Server() {
	}
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		//checks to see if you can communicate with the server  
		databaseHandlerService.startDatabaseHandler(new AsyncCallback<Void>() {
			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error: Failed to start database handler. Please try reloading the page. " + relogString);
				return;				
			}
		});
		
		String code = com.google.gwt.user.client.Window.Location.getParameter("code");
		if(code!=null)
		{
			databaseHandlerService.confirm(code, new AsyncCallback<Boolean>() {
				@Override
				public void onSuccess(Boolean result) {
					// TODO Auto-generated method stub
					if(result)
						Window.alert("Registration successful.  Please log in.");
				}
				@Override
				public void onFailure(Throwable caught) {
					Window.alert("Error: registration communication error");
					return;				
				}
			});
		}
		
		//display redcross logo header 
		RootPanel imageHeader = RootPanel.get("imageHeader");
		imageHeader.add(new ImageHeader());			
		header = RootPanel.get("header");
		
		// uncomment this code to get login screen
	    if ( sessionID != null )
	    {
	    	checkWithServerIfSessionIdIsStillLegal();
	    }
	    else
	    {
			ContentPanel.getInstance().setContent(new LoginContent());
	    }
		
	}
	
	private void checkWithServerIfSessionIdIsStillLegal()
	{
		int sessionID1 = Integer.parseInt(sessionID); 
		databaseHandlerService.checkWithServerIfSessionIdIsStillLegal(sessionID1, new AsyncCallback<Boolean>(){

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Cannot connect the server");
					}

					@Override
					public void onSuccess(Boolean result) {
						if (result)
						{
							isAdmin();
						}
						else 
						{
							ContentPanel.getInstance().setContent(new LoginContent());
						}
					}
			
		});
	}
	
	/*
	 * this function checks the see if the person is an admin if the person is still logged in*/
	private void isAdmin()
	{
		int sessionID1 = Integer.parseInt(sessionID); 
		databaseHandlerService.getUserFromSessionId(sessionID1, new AsyncCallback<Person>(){

				@Override
				public void onFailure(Throwable caught) {
					Window.alert("Cannot connect to the server.");
				}

				@Override
				public void onSuccess(Person result) {
					if (result == null) {
						Window.alert("Cannot connect to the server.");
						return;
					}
					ImageHeader.hprlnkLogOut.setVisible(true);
					if (result.username.equals("dev")) {
						header.add(new DeveloperHeader());
						header.setVisible(true);
						ContentPanel.getInstance().setContent(new HomeContent());
						return;
					}
					
					ContentPanel.getInstance().setContent(new HomeContent());
					currentUser = result;
					if (result instanceof Administrator)
					{
						header.add(new AdminHeader());
						header.setVisible(true);
						ContentPanel.getInstance().setContent(new HomeContent());
					}
					else 
					{
						header.add(new VolunteerHeader());
						header.setVisible(true);
						ContentPanel.getInstance().setContent(new HomeContent());
					}
				}
			
		});
	}
}
